//
//  NYPostViewController.m
//  BS
//
//  Created by neal on 15/8/21.
//  Copyright (c) 2015年 Neal. All rights reserved.
//

#import "NYPostViewController.h"
#import "NYPlaceholderTextView.h"
#import "NYPostWordToolBar.h"

@interface NYPostViewController ()<UITextViewDelegate>
/*  */
@property(nonatomic , weak) NYPlaceholderTextView *textView;

@property(nonatomic , weak) UIView *toolbar;
@end

@implementation NYPostViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNav];
    [self setupTextView];
    [self setupToolBar];
    
}
#pragma mark - 设置导航条
- (void)setupNav{
    self.navigationItem.title = @"发布消息";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style: UIBarButtonItemStyleDone target:self action:@selector(cancel)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"发布" style: UIBarButtonItemStyleDone target:self action:@selector(post)];
    self.navigationItem.rightBarButtonItem.enabled = NO;
    [self.navigationController.navigationBar layoutIfNeeded];

}
#pragma mark - 设置textView
- (void)setupTextView{
    NYPlaceholderTextView *textView = [[NYPlaceholderTextView alloc]initWithFrame:self.view.bounds];
    textView.placeholder = @"把好玩的图片，好笑的段子或糗事发到这里，接受千万网友膜拜吧！发布违反国家法律内容的，我们将依法提交给有关部门处理。";
    textView.delegate = self;
    textView.alwaysBounceVertical = YES;
    textView.frame = self.view.bounds;
    [self.view addSubview:textView];
    self.textView = textView;
    [textView becomeFirstResponder ];

}
#pragma mark - setupToolBar
- (void)setupToolBar{
    NYPostWordToolBar *toolbar =[NYPostWordToolBar viewFromXib];
    toolbar.width = self.view.width;
    toolbar.y = self.view.height - toolbar.height;
    [self.view addSubview:toolbar];
    self.toolbar = toolbar;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboaedDidChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

}
- (void)keyboaedDidChangeFrame:(NSNotification *)note
{
    double duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        CGFloat keyboardY = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y;
        CGFloat toobarY = keyboardY - NYScreenH;
        NSLog(@"%f",toobarY);

        self.toolbar.transform = CGAffineTransformMakeTranslation(0, toobarY);
        
    }];
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)cancel
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)post
{
    
}
- (void)textViewDidChange:(UITextView *)textView{
    self.navigationItem.rightBarButtonItem.enabled = textView.hasText;
}
@end
